*------------------------------------------------------------------------------*
*** POLLUTION AND WEATHER VALUES IN THE 30 DAYS BEFORE AND AFTER ELECTIONS ***
*------------------------------------------------------------------------------*

***Locals
local lander "Baden_Württemberg Bayern Berlin Brandenburg Bremen Hamburg Hessen Mecklenburg_Vorpommern Niedersachsen Nordrhein_Westfalen Rheinland_Pfalz Saarland Sachsen Sachsen_Anhalt Schleswig_Holstein Thüringen"
local vars_UBA " PM10_ug O3_ug "
local vars_DWD " temp humid_rel wind_speed precip " 

*------------------------------------------------------------------------------*

***Dates
use "${tempdata}kreise_complete_all.dta", clear

keep if samplePM10_allcontrols == 1

preserve
	keep election_date
	bys election_date: keep if _n==1
	save "${tempdata}election_dates_est_sample.dta", replace
restore

foreach l of local lander {
	preserve
		keep if Land == "`l'"
		keep election_date Land

		bys election_date: keep if _n==1
		
		expand 61
		sort election_date

		bys election_date: gen n_date = _n

		forval n = 32/61 {
		replace election_date = election_date + dofc(86400000*(`n'-31)) if n_date == `n'
		}

		forval n = 1/30 {
		replace election_date = election_date - dofc(86400000*`n') if n_date == `n'
		}
		
		gen el = 1 if n_date == 31

		bys election_date el: keep if _n==1
		bys election_date: keep if _n==1

		drop n_date

		gen n_date_`l' = _n

		count
		local N_date_`l' = r(N)

		merge 1:1 election_date using "${tempdata}election_dates_est_sample.dta", keep(master match) nogen
		
		rename election_date date
		
		gen year 	= year(date)
		gen month 	= month(date)
		gen day 	= day(date)
		
		qui compress
		save "${tempdata}dates_`l'.dta", replace
	restore
} // l

*------------------------------------------------------------------------------*

***Pollution
use station longitude latitude using "${rawdata}pollution/meta_station_UBA.dta", clear

count
local N_UBA = r(N)

*------------------------------------------------------------------------------*

***Weather
use station_DWD longitude latitude using "${rawdata}pollution/meta_station_DWD.dta", clear

count
local N_DWD = r(N)

*------------------------------------------------------------------------------*

***Loop over pollution and weather data
foreach l of local lander {
	
	foreach st in UBA DWD {

		use "${tempdata}/`l'_centroids.dta", clear
	
		*Create all possible combinations of municipalities and pollution stations
		expand `N_`st''
	
		bys kreis_code: gen n_`st' = _n

		merge m:1 n_`st' using "${tempdata}stations_`st'.dta", keep(master match) nogen 

		rename longitude_d longitude_kreis
		rename latitude_d  latitude_kreis

		*Compute distance
		distance latitude_kreis longitude_kreis latitude_`st' longitude_`st' , a(kreis) b(station)
	
		*Restrict to muni-monitor combinations within certain radius
		keep if dist_KRST <= 30
	

		*Merge election dates 
		expand `N_date_`l''
		bys kreis_code station_`st' : gen n_date_`l' = _n

		merge m:1 n_date_`l' using "${tempdata}dates_`l'.dta", keep(match) nogen


		**Merge pollution/weather on election day		

		*Merge
	
		if "`st'"=="UBA" rename station_UBA station
	
		if "`st'"=="UBA" merge m:1 station 		year month day using "${tempdata}pollution_UBA_d_19902019_w_av.dta", keepusing(`vars_`st'') keep(master match) nogen
		if "`st'"=="DWD" merge m:1 station_DWD 	year month day using "${tempdata}weather_d_19802019_w_av.dta", keepusing(`vars_`st'') keep(master match) nogen
	
		if "`st'"=="UBA" recode `vars_`st'' (min/0 = 0)


		**Compute inverse-distance weighted mean for pollutants
		
		foreach p of varlist `vars_`st'' {
			
			preserve

			keep if `p'!=.

			*Create weight
			gen double weight = 1/dist_KRST
			bys kreis_code date: egen double weight_tot = total(weight)
			replace weight = weight/weight_tot
			drop weight_tot

			*Weighted mean
			bys kreis_code date: egen double `p'_weight = total(`p'*weight)
			bys kreis_code date: keep if _n==1

			keep kreis_code Kreis date el `p'_weight
		
			rename `p'_weight `p'_weight

			qui compress
			save "${tempdata}/`l'_`st'_`p'.dta", replace

			restore
		} // p

	} // st

} // l

*------------------------------------------------------------------------------*

***Creare full pollution and weather dataset for EACH Lander
local vars_UBA_2 " O3_ug "

foreach l of local lander{

	use "${tempdata}/`l'_UBA_PM10_ug.dta", clear

	*Merge pollution
	foreach p of local vars_UBA_2 {
		merge 1:1 kreis_code date using "${tempdata}/`l'_UBA_`p'.dta", nogen
	} // p

	*Merge weather
	foreach w of local vars_DWD {
		merge 1:1 kreis_code date using "${tempdata}/`l'_DWD_`w'.dta", nogen
	} // w
	
	*Save
	qui compress
	save "${tempdata}/`l'_p_w_electiondates_plusminus30days.dta", replace
	
	*Esare files
	foreach p of local vars_UBA {
		erase "${tempdata}/`l'_UBA_`p'.dta"
	} // p

	foreach w of local vars_DWD {
		erase "${tempdata}/`l'_DWD_`w'.dta"
	} // w

} // l

*------------------------------------------------------------------------------*

**Create full pollution and weather dataset for ALL Lander
foreach l of local lander {
	append using "${tempdata}/`l'_p_w_electiondates_plusminus30days.dta"
} // l

*Remove duplicate entries
bys kreis_code date: keep if _n == 1

*Save
qui compress
save "${tempdata}kreise_p_w_electiondates_plusminus30days.dta", replace

*Erase
foreach l of local lander {
	erase "${tempdata}/`l'_p_w_electiondates_plusminus30days.dta"
	erase "${tempdata}dates_`l'.dta"
} // l

*------------------------------------------------------------------------------*

clear

exit
